package cn.jietuo.first.cartoonalgorithm;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.OptionalDouble;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/7/27
 * @description:
 */
public class B  extends A{

    public static void main(String[] args) {

        map();
//        parallel();
//        collectors();
//        statistics();
    }


    public static void map(){
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        // 获取对应的平方数
        List<Integer> squaresList = numbers.stream().map( i -> i*i).collect(Collectors.toList());
        squaresList.stream().forEach(System.out::println);
    }

    public static void parallel(){
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        int count = (int) strings.parallelStream().filter(string -> !string.isEmpty()).count();
        System.out.println(count);


        Stream.of("little","red","riding","hood")
                .parallel()
                .map(s -> {
                    System.out.println("map:" + s + " " + Thread.currentThread().getName());
                    return s+"_";
                })
                .filter(s -> {
                    System.out.println("filter: " + s + " " + Thread.currentThread().getName());
                    return s.length() > 3;
                })
                .reduce((s1,s2) ->{
                    System.out.println("redicer:" + s1 + " " + Thread.currentThread().getName());
                    return s1.length() > s2.length() ? s1:s2;
                });
    }

    public static void collectors(){
        List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());

        System.out.println("筛选列表: " + filtered);
        String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
        System.out.println("合并字符串: " + mergedString);

        String collect = strings.stream().filter(s -> !s.isEmpty()).collect(Collectors.joining(",", "haha", "hehe"));
        System.out.println(collect);
    }

    public  static void statistics(){
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);

        IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();

        System.out.println("列表中最大的数 : " + stats.getMax());
        System.out.println("列表中最小的数 : " + stats.getMin());
        System.out.println("所有数之和 : " + stats.getSum());
        System.out.println("平均数 : " + stats.getAverage());

        OptionalDouble average = numbers.stream().mapToInt(x -> x).average();
        System.out.println(average.getAsDouble());
    }
}
